// ring.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "correctness_test.h"
#include "queue_test.h"
#include "stack_test.h"
#include "linklist_test.h"
#include <sstream>
#include <fstream>


#define count_total	1000000
DWORD g_proccessors_count = 0;

//---------------------------------------------------------------------------
template<typename test_t>
void execute_test(int count_to, bool is_ring_on, bool force_context_switch)
{
	test_t test(count_to, force_context_switch);

	for(int processors_to_use = 1; processors_to_use <= (int)g_proccessors_count; processors_to_use++)
	{
		std::stringstream csv;

		int threads_per_processor = 1;
		for(threads_per_processor = 1; threads_per_processor <= (int)g_proccessors_count; threads_per_processor++)
		{
			DWORD result = test.go(processors_to_use, threads_per_processor, is_ring_on);

			csv << threads_per_processor << "," << result << std::endl;
		}

		// write to file
		std::ofstream csv_file;
		std::stringstream log_name;
		log_name << ".\\results\\";
		log_name << test.test_name() << "_processors_" << processors_to_use << "_ring_" << is_ring_on ? "on" : "off";
		if(force_context_switch){ log_name << "_with_context_switch"; }
		log_name << ".csv";
		csv_file.open(log_name.str());
		csv_file << csv.rdbuf();

		csv_file.flush();
		csv_file.close();
	}
}
//---------------------------------------------------------------------------
int _tmain(int argc, _TCHAR* argv[])
{
	try
	{
		g_proccessors_count = tools::get_processors_count();
		printf("processors has been detected: %u\r\n", g_proccessors_count);
		
		/*
		correctness_test ct;
		ct.go();
		*/
		
		execute_test<queue_test>(count_total, ring_off, true);
		execute_test<queue_test>(count_total, ring_on, true);
		execute_test<queue_test>(count_total, ring_off, false);
		execute_test<queue_test>(count_total, ring_on, false);

		execute_test<stack_test>(count_total, ring_off, true);
		execute_test<stack_test>(count_total, ring_on, true);
		execute_test<stack_test>(count_total, ring_off, false);
		execute_test<stack_test>(count_total, ring_on, false);

		execute_test<linklist_test>(count_total, ring_off, true);
		execute_test<linklist_test>(count_total, ring_on, true);
		execute_test<linklist_test>(count_total, ring_off, false);
		execute_test<linklist_test>(count_total, ring_on, false);

		printf("Executed all tests.");
	}
	catch (std::exception& e)
	{
		printf(e.what());
	}
	catch(...)
	{
		printf("unknown\r\n");
	}
	
	return 0;
}
//---------------------------------------------------------------------------
